package pre_processing;


import java.util.ArrayList;

import util.Util;

public class Stemming
{
	/**
	 * variabel yang menyimpan array dari kata dasar
	 */
	private	static ArrayList<String> arrayOfRoot = new ArrayList<String>();
	
	/**
	 * variabel yang menyimpan status pencarian kata dasar
	 */
	private static boolean			  isFindRoot;
	
	/**
	 * variabel yang menyimpan inflection suffix
	 */
	private static String			  infsuffix;
	
	/**
	 * variabel yang menyimpan derivation suffix
	 */
	private static String			  dersuffix;	
	
	/**
	 * variabel yang menyimpan imbuhan belakang
	 */
	private static String[]		  prefix = new String[3];
	
	/**
	 * variabel yang menyimpan huruf konsonan
	 */
	private static String			  consonant = "bcdfghjklmnpqrstvwxyz";	
	
	/**
	 * variabel yang menyimpan huruf vokal
	 */
	private static String			  vowel = "aiueo";
	
	/**
	 * variabel yang menyimpan semua alfabet
	 */
	private static String			  all = "abcdefghijklmnopqrstuvwxyz";
	
	
	/**
	 * method utama yang melakukan proses stemming
	 * @param arrayOfOrig array dari kata-kata aslinya
	 * @return arrayOfRoot array dari kata-kata hasil stemming
	 */
	public static String doStemming (String word)
	{
		/**
		 * hilangkan semua isi dari variabel arrayOfRoot 
		 * dan inisialisasi variabel isFindRoot dengan nilai false
		 */
		arrayOfRoot.clear();
		isFindRoot = false;
		
		/**
		 * Inisialisasi penampung imbuhan
		 */
		prefix[0] = "";
		prefix[1] = "";
		prefix[2] = "";
		
		String result;
		
		// loop untuk melakukan stemming kata satu persatu
			/**
			 * kalau panjang kata <=2
			 */
			if (word.length()<=2)
			{
				isFindRoot = true;
			}
			else
				/**
				 * kalau panjang kata lebih dari 2
				 */
			{
				/**
				 * algoritma nazief adriani
				 */
				
				/**
				 * 1. periksa apakah kata asli sudah merupakan kata dasar atau tidak, 
				 * jika ya lanjutkan ke kata selanjutnya, jika tidak lakukan langkah selanjutnya
				 */
				
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(word.hashCode());
			}
			
			/**
			 * variabel penampung word
			 */
			String OrigWord = word;
			
			/**
			 * masuk ke if jika bukan kata dasar
			 */
			if (!isFindRoot)
			{
				/**
				 *  2. hapus inflection suffix (-lah, -kah, -tan, -pun, -ku, -mu, -nya)
				 */
				word = delInflectionSuffix(word);
//				System.out.println("Inflection : " + word);
				if (!isFindRoot)
				{
					// 3. hapus derivational suffix (-i, -an, -kan)
					word = delDerivationSuffix(word);
//					System.out.println("Derivation Suffix : " + word);
					if (!isFindRoot)
					{
						// 4. hapus derivational prefix (di-, ke-, se-, te-, be-, me-, pe-)
						for (int i = 0; i<=2; i++)
						{
							word = delDerivationPrefix(word,i);
//							System.out.println("Derivation Prefix : " + word);
							if (isFindRoot)
							{
								result = word;
								break;
							}
							else if (i == 2)
							{
								if (!dersuffix.equals(""))
								{
//									isFindRoot = isRootWord(word+dersuffix, index);
									isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word+dersuffix).hashCode());
									if (isFindRoot)
									{
										result = word+dersuffix;
										break;
									}
//									isFindRoot = isRootWord(word+infsuffix, index);
									isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word+infsuffix).hashCode());
									if (isFindRoot)
									{
										result = word+infsuffix;
										break;
									}
								}
								result = OrigWord;
								break;
							}
						}
						
					}
					else
					{
						result = word;
					}
				}
				else
				{
					result = word;
				}
			}
			else
			{
				result = word;
			}
		return word;
	}
	
	
	/**
	 * method menghilangkan inflection suffix
	 * @param word
	 * @return word
	 */
	private static String delInflectionSuffix(String word)
	{
		/**
		 * variabel penampung suffix
		 */
		infsuffix = "";
		
		/**
		 * periksa akhiran kata -lah, -kah, -tah, -pun, -ku, -mu, -nya
		 */
		if (word.endsWith("lah") || word.endsWith("kah") || word.endsWith("tah") || word.endsWith("pun"))
		{
			/**
			 * variabel infsuffix diisi imbuhan belakang
			 */
			infsuffix = word.substring(word.length()-3);
			
			/**
			 * menghilangkan 3 huruf paling belakang (imbuhan/afiks)
			 */
			word = word.substring(0, word.length()-3);
			
			/**
			 * periksa apakah kata asli sudah merupakan kata dasar atau tidak, 
			 */
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
			
			/**
			 * jika kata bukan kata dasar
			 */
			if (!isFindRoot)
			{
				/**
				 * periksa akhiran kata -ku, -mu, -nya
				 */
				if (word.endsWith("ku") || word.endsWith("mu"))
				{
					/**
					 * variabel infsuffix diisi imbuhan belakang
					 */
					infsuffix = word.substring(word.length()-2);
					
					/**
					 * menghilangkan 3 huruf paling belakang (imbuhan/afiks)
					 */
					word = word.substring(0, word.length()-2); 

					/**
					 * periksa apakah kata asli sudah merupakan kata dasar atau tidak, 
					 */
					isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
					return word;
				}
				else if (word.endsWith("nya"))
				{
					infsuffix = word.substring(word.length()-3);
					word = word.substring(0, word.length()-3); // menghilangkan 3 huruf paling belakang (imbuhan/afiks)
//					isFindRoot = isRootWord(word,index); // periksa apakah sudah merupakan kata das,indexar
					isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
					return word;
				}
				else
				{
					return word;
				}
			}
			else
			{
				return word;
			}
		}
		else if (word.endsWith("ku") || word.endsWith("mu"))
		{
			infsuffix = word.substring(word.length()-2);
			word = word.substring(0, word.length()-2); // menghilangkan 2 huruf paling belakang (imbuhan/afiks)
//			isFindRoot = isRootWord(word,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
			return word;
		}
		else if (word.endsWith("nya"))
		{
			infsuffix = word.substring(word.length()-3);
			word = word.substring(0, word.length()-3); // menghilangkan 3 huruf paling belakang (imbuhan/afiks)
//			isFindRoot = isRootWord(word,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
			return word;
		}
		else
		{
			return word;
		}
	}
	
	/**
	 * method menghilangkan derivation suffix
	 * @param word
	 * @return word
	 */
	private static String delDerivationSuffix(String word)
	{
		String tempWord;
		dersuffix = ""; // inisialisasi suffix dengan string kosong
		// periksa akhiran kata -i, -an, -kan
		if (word.endsWith("i"))
		{
			tempWord = word.substring(0, word.length()-1); // menghilangkan 1 huruf paling belakang (imbuhan/afiks)
//			isFindRoot = isRootWord(tempWord,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((tempWord).hashCode());
			dersuffix = "i"; // simpan imbuhan yang dibuang
			// jika ditemukan
			if (isFindRoot)
				return tempWord;
			// jika tidak ditemukan
			else
			{
				// periksa kombinasi awalan dan akhiran, jika benar (dissalowed) maka return kata awal, jika salah return kata tanpa sufiks
				if (checkDisallowedSuffix(word))
					return word;
				else
					return tempWord;
			}
		}
		else if (word.endsWith("kan"))
		{
			tempWord = word.substring(0, word.length()-3); // menghilangkan 3 huruf paling belakang (imbuhan/afiks)
//			isFindRoot = isRootWord(tempWord,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((tempWord).hashCode());
			dersuffix = "kan"; // simpan imbuhan yang dibuang
			// jika ditemukan
			if (isFindRoot)
				return tempWord;
			// jika tidak ditemukan
			else
			{
				// periksa kombinasi awalan dan akhiran, jika benar (dissalowed) maka return kata awal, jika salah return kata tanpa sufiks
				if (checkDisallowedSuffix(word))
					return word;
				else
					return tempWord;
			}
		}
		else if (word.endsWith("an"))
		{
			tempWord = word.substring(0, word.length()-2); // menghilangkan 2 huruf paling belakang (imbuhan/afiks)
//			isFindRoot = isRootWord(tempWord,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((tempWord).hashCode());
			dersuffix = "an"; // simpan imbuhan yang dibuang
			// jika ditemukan
			if (isFindRoot)
				return tempWord;
			// jika tidak ditemukan
			else
			{
				// periksa kombinasi awalan dan akhiran, jika benar (dissalowed) maka return kata awal, jika salah return kata tanpa sufiks
				if (checkDisallowedSuffix(word))
					return word;
				else
					return tempWord;
			}
		}
		else
		{
			return word;
		}
	}
	
	private static boolean checkDisallowedSuffix(String word) 
	{
		if (word.startsWith("be") && word.endsWith("i"))
			return true;
		else if (word.startsWith("di") && !(word.endsWith("kan")) && word.endsWith("an"))
			return true;
		else if (word.startsWith("ke") && (word.endsWith("i") || word.endsWith("kan")))
			return true;
		else if (word.startsWith("me") && !(word.endsWith("kan")) && word.endsWith("an"))
			return true;
		else if (word.startsWith("se") && (word.endsWith("i") || word.endsWith("kan")))
			return true;
		else
			return false;
	}

	/**
	 * method menghilangkan derivation prefix
	 * @param word
	 * @return word
	 */
	private static String delDerivationPrefix(String word, int loop)
	{
		prefix[loop] = ""; // inisialisasi prefix dengan string kosong
		if (loop > 0 && !prefix[loop-1].equals(""))
		{
			if (word.substring(0, prefix[loop-1].length()).equals(prefix[loop-1]))
				return word;
		}
		// periksa awalan kata be-, di-, ke-, me-, pe-, se-, te-
		if (word.startsWith("di") || word.startsWith("ke") || word.startsWith("se"))
		{
			// simpan prefix yang digunakan untuk pemeriksaan prefix pada loop selanjutnya
			if (word.startsWith("di"))
				prefix[loop] = "di";
			else if (word.startsWith("ke"))
				prefix[loop] = "ke";
			else if (word.startsWith("se"))
				prefix[loop] = "se";
			word = word.substring(2, word.length()); // menghilangkan 2 huruf paling depan (imbuhan/afiks)
//			isFindRoot = isRootWord(word,index); // periksa apakah sudah merupakan kata das,indexar
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((word).hashCode());
			return word;
		}
		else if (word.startsWith("te"))
		{
			prefix[loop] = "te";
			word = getPrefixTe(word);
		}
		else if (word.startsWith("be"))
		{
			prefix[loop] = "be";
			word = getPrefixBe(word);
		}
		else if (word.startsWith("me"))
		{
			prefix[loop] = "me";
			word = getPrefixMe(word);
		}
		else if (word.startsWith("pe"))
		{
			prefix[loop] = "pe";
			word = getPrefixPe(word);
		}
		return word;		
	}
	
	private static String getBackSuffix(int wordLength)
	{
		if (wordLength < 3)
		{
			if (!dersuffix.equals(""))
				return dersuffix;
			else
				return infsuffix;
		}
		return "";
	}
	
	/**
	 * 
	 * @param word
	 * @return
	 */
	private static String getPrefixBe(String word) 
	{
		String termWord;
		if (word.startsWith("ber"))
		{
			termWord = word.substring(3);
			termWord = termWord + getBackSuffix(termWord.length());
			if (vowel.contains(termWord.substring(0,1)))
			{
				// MASIH AMBIGU UNTUK RETURN
//				isFindRoot = isRootWord("r"+termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("r"+termWord).hashCode());
				if (isFindRoot)
					return "r"+termWord;
				else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode()))
				{
					isFindRoot = true;
					return termWord;
				}
				return "r"+termWord;
			}
			else if (!(termWord.substring(0,1).equals("r")) && !(termWord.substring(2,4).equals("er")) && consonant.contains(termWord.substring(0,1)) && all.contains(termWord.substring(1,2)))
			{
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
			else if (!(termWord.substring(0,1).equals("r")) && termWord.substring(2,4).equals("er") && consonant.contains(termWord.substring(0,1)) && all.contains(termWord.substring(1,2)) && vowel.contains(termWord.substring(4,5)))
			{
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
		}
		else if (word.startsWith("bel"))
		{
			termWord = word.substring(3);
			if (termWord.startsWith("ajar"))
			{
				isFindRoot = true;
				return termWord;
			}
		}
		else if (word.startsWith("be"))
		{
			termWord = word.substring(2);
			termWord = termWord + getBackSuffix(termWord.length());
			if (consonant.contains(word.substring(2,3)) && word.substring(3,5).equals("er") && consonant.contains(word.substring(5,6)))
			{
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
		}
		return word;
	}

	/**
	 * 
	 * @param word
	 * @return
	 */
	private static String getPrefixTe(String word) 
	{
		String termWord;
		if (word.startsWith("ter"))
		{
			termWord = word.substring(3);
			termWord = termWord + getBackSuffix(termWord.length());
			if (vowel.contains(termWord.substring(0,1)))
			{
				// MASIH AMBIGU UNTUK RETURN
//				isFindRoot = isRootWord("r"+termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("r"+termWord).hashCode());
				if (isFindRoot)
					return "r"+termWord;
				else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode()))
				{
					isFindRoot = true;
					return termWord;
				}
				return "r"+termWord;
			}
			else if (!(termWord.substring(0,1).equals("r")) && termWord.substring(1,3).equals("er") && consonant.contains(termWord.substring(0,1)) && vowel.contains(termWord.substring(3,4)))
			{
//				isFindRoot = isRootWord(termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
			else if (!(termWord.substring(0,1).equals("r")) && !(termWord.substring(1,3).equals("er")) && consonant.contains(termWord.substring(0,1)))
			{
//				isFindRoot = isRootWord(termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
		}
		else if (word.startsWith("te"))
		{
			termWord = word.substring(2);
			termWord = termWord + getBackSuffix(termWord.length());
			if (!(termWord.substring(0,1).equals("r")) && termWord.substring(1,3).equals("er") && consonant.contains(termWord.substring(0,1)) && consonant.contains(termWord.substring(3,4)))
			{
//				isFindRoot = isRootWord(termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
		}
		return word;
	}

	/**
	 * 
	 * @param word
	 * @return
	 */
	private static String getPrefixMe(String word) 
	{
		String termWord;
		if (word.startsWith("mempe") && word.substring(5,6).equals("r") || word.substring(5,6).equals("l"))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("memp") && vowel.contains(word.substring(4,5)))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("me") && (word.substring(2,3).equals("l") || word.substring(2,3).equals("r") || word.substring(2,3).equals("w") || word.substring(2,3).equals("y")) && vowel.contains(word.substring(3,4)))
		{
			termWord = word.substring(2);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("mem") && word.substring(3,4).equals("b") || word.substring(3,4).equals("f") || word.substring(3,4).equals("v"))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("mem") && (word.substring(3,4).equals("r") && vowel.contains(word.substring(4,5))) || vowel.contains(word.substring(3,4)))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(3);
//			isFindRoot = isRootWord("m"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("m"+termWord).hashCode());
			if (isFindRoot)
				return "m"+termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("p"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "p"+termWord;
			}
			return "m"+termWord;
		}
		else if (word.startsWith("men") && (word.substring(3,4).equals("c") || word.substring(3,4).equals("d") || word.substring(3,4).equals("j") || word.substring(3,4).equals("z")))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("men") && vowel.contains(word.substring(3,4)))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(3);
//			isFindRoot = isRootWord("t"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("t"+termWord).hashCode());
			if (isFindRoot)
				return "t"+termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("n"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "n"+termWord;
			}
			return "t"+termWord;
		}
		else if (word.startsWith("meng") && (word.substring(4,5).equals("g") || word.substring(4,5).equals("h") || word.substring(4,5).equals("q")))
		{
			termWord = word.substring(4);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("meng") && (vowel.contains(word.substring(4,5))))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(4);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			if (isFindRoot)
				return termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("k"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "k"+termWord;
			}
			return termWord;
		}
			
		else if (word.startsWith("meny"))
		{
			termWord = word.substring(4);
			if (vowel.contains(word.substring(4,5)))
			{
//				isFindRoot = isRootWord("s"+termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("s"+termWord).hashCode());
				return "s"+termWord;
			}
		}
			
		return word;
	}
	
	/**
	 * 
	 * @param word
	 * @return
	 */
	private static String getPrefixPe(String word) 
	{
		String termWord;
		if (word.startsWith("pe") && (word.substring(2,3).equals("w") || word.substring(2,3).equals("y")) && vowel.contains(word.substring(3,4)))
		{
			termWord = word.substring(2);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("per") && vowel.contains(word.substring(3,4)))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			if (isFindRoot)
				return termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("r"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "r"+termWord;
			}
			return termWord;
		}
		else if (word.startsWith("per") && !(word.substring(3,4).equals("r")) && consonant.contains(word.substring(3,4)) && all.contains(word.substring(4,5)) && !(word.substring(5,7).equals("er")))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("per") && !(word.substring(3,4).equals("r")) && consonant.contains(word.substring(3,4)) && all.contains(word.substring(4,5)) && word.substring(5,7).equals("er") && vowel.contains(word.substring(7,8)))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("pem") && (word.substring(3,4).equals("b") || word.substring(3,4).equals("f") || word.substring(3,4).equals("v")))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("pem") && ((word.substring(3,4).equals("r") && vowel.contains(word.substring(4,5))) || vowel.contains(word.substring(3,4))))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(3);
//			isFindRoot = isRootWord("m"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("m"+termWord).hashCode());
			if (isFindRoot)
				return "m"+termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("p"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "p"+termWord;
			}
			return "m"+termWord;
		}
		else if (word.startsWith("pen") && (word.substring(3,4).equals("c") || word.substring(3,4).equals("d") || word.substring(3,4).equals("j") || word.substring(3,4).equals("z")))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("pen") && vowel.contains(word.substring(3,4)))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(3);
//			isFindRoot = isRootWord("n"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("n"+termWord).hashCode());
			if (isFindRoot)
				return "n"+termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("t"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "t"+termWord;
			}
			return "n"+termWord;
		}
		else if (word.startsWith("peng") && (word.substring(4,5).equals("g") || word.substring(4,5).equals("h") || word.substring(4,5).equals("q")))
		{
			termWord = word.substring(4);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("peng") && vowel.contains(word.substring(4,5)))
		{
			// MASIH AMBIGU UNTUK RETURN
			termWord = word.substring(4);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			if (isFindRoot)
				return termWord;
			else if (isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("k"+termWord).hashCode()))
			{
				isFindRoot = true;
				return "k"+termWord;
			}
			return termWord;
		}
		else if (word.startsWith("peny") && vowel.contains(word.substring(4,5)))
		{
			termWord = word.substring(4);
//			isFindRoot = isRootWord("s"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("s"+termWord).hashCode());
			return "s"+termWord;
		}
		else if (word.startsWith("pel") && vowel.contains(word.substring(3,4)))
		{
			termWord = word.substring(3);
			if (termWord.startsWith("ajar"))
			{
//				isFindRoot = isRootWord(termWord,index);
				isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
				return termWord;
			}
//			isFindRoot = isRootWord("l"+termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist(("l"+termWord).hashCode());
			return "l"+termWord;
		}
		else if (word.startsWith("pe") && word.substring(3,5).equals("er") && !(word.substring(2,3).equals("r")) && !(word.substring(2,3).equals("w")) && !(word.substring(2,3).equals("y")) && !(word.substring(2,3).equals("l")) && !(word.substring(2,3).equals("m")) && !(word.substring(2,3).equals("n")) && consonant.contains(word.substring(2,3)) && vowel.contains(word.substring(5,6)))
		{
			termWord = word.substring(3);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		else if (word.startsWith("pe") && !(word.substring(3,5).equals("er")) && !(word.substring(2,3).equals("r")) && !(word.substring(2,3).equals("w")) && !(word.substring(2,3).equals("y")) && !(word.substring(2,3).equals("l")) && !(word.substring(2,3).equals("m")) && !(word.substring(2,3).equals("n")) && consonant.contains(word.substring(2,3)))
		{
			termWord = word.substring(2);
//			isFindRoot = isRootWord(termWord,index);
			isFindRoot = Util.getTreeKamusBahasaIndonesia().isExist((termWord).hashCode());
			return termWord;
		}
		return word;
	}

}
